import projectSetting from '@/settings/projectSetting.js'
/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string | null}
 */
export function parseTime(time, cFormat) {
  time = time || 0
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
      time = parseInt(time)
    }
    if (typeof time === 'number' && time.toString().length === 10) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  // eslint-disable-next-line camelcase
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    return value.toString().padStart(2, '0')
  })
  // eslint-disable-next-line camelcase
  return time_str
}

/**
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatTime(time, option) {
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000
  } else {
    time = +time
  }
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return parseTime(time, option)
  } else {
    return (
      d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分'
    )
  }
}

/**
 * 到期天数
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatExpireDay(time, over) {
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000
  } else {
    time = +time
  }

  const d = new Date().getTime()
  const diff = (time - d) / 1000

  if (diff < 0) {
    return '已过期'
  } else if (over) {
    return '还有' + Math.ceil(diff / (24 * 3600)) + '天过期'
  } else {
    return '剩余' + Math.ceil(diff / (24 * 3600)) + '天'
  }
}

/**
 * 到期时间
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatExpireTime(time, cFormat) {
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000
  } else {
    time = +time
  }
  const d = new Date().getTime()
  const diff = (time - d) / 1000
  if (diff < 0) {
    return '已过期'
  } else {
    return parseTime(time, cFormat)
  }
}

// 有效期/天
export function formatTermTime(time, cFormat) {
  let d = new Date().getTime()
  let t = new Date()
  let n = t.setDate(new Date().getDate() + time)
  const diff = (n - d) / 1000
  if (diff < 1) {
    return '已过期'
  } else {
    return parseTime(n, cFormat)
  }
}

/**
 * 将秒转化为时分秒
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatSeconds(s) {
  s = Math.floor(s)
  var day = Math.floor(s / (24 * 3600))
  var hour = Math.floor((s - day * 24 * 3600) / 3600)
  var minute = Math.floor((s - day * 24 * 3600 - hour * 3600) / 60)
  var second = s - day * 24 * 3600 - hour * 3600 - minute * 60
  if (hour < 10) {
    hour = '0' + hour
  }
  if (minute < 10) {
    minute = '0' + minute
  }
  if (second < 10) {
    second = '0' + second
  }
  if (hour > 0) {
    return hour + ':' + minute + ':' + second
  }
  return minute + ':' + second
}

/**
 * 货币符号
 * @param {String,number} val
 * @param {String} unit 后面单位(元)
 */
export function formatMoney(val, unit) {
  if (unit) {
    return val + unit
  }
  return projectSetting.appMoneySymbol + floatCount(val)
}

/**
 * 浮点数
 * @param {String,number} val
 */
export function floatCount(val) {
  return parseFloat(val)
}

/**
 * 掩码 147****2222
 * @param { String } val 掩码字符
 * @param { number } start 默认显示前3位
 * @param { number } end 默认显示后4位
 */
export function formatMask(val, start, end) {
  val = val.toString() // 转换成字符串
  start = 3 // 前3位
  end = 4 // 后4位
  let len = val.length - start - end
  let symbol = ''
  for (var i = 0; i < len; i++) {
    symbol += '*'
  }
  return val.substring(0, start) + symbol + val.substring(val.length - end)
}
